from rest_framework.exceptions import APIException
from rest_framework.permissions import BasePermission

from apps.Blog_User.Errors import UserNotLoginError, UserLoginPermissionError
from apps.Blog_User.models import UserModels, UserTokenModel


class UserPhoneRepeatCount(BasePermission):
    def has_object_permission(self, request, view, obj):
        pass

    def has_permission(self, request, view):
        print("ok")
        # if UserModels.UserPhoneHaves(UserPhone=request.data.get['phone']):
        #     raise UserPhoneRepeatCount
        # else:
        return True


class UserInfoPermission(BasePermission):
    def has_permission(self, request, view):
        _user_info = UserTokenModel.get_user_token(user_token=request.query_params.get("token", None))
        if not _user_info:
            raise UserNotLoginError
        else:
            return True

    def has_object_permission(self, request, view, obj):
        return True


class UserLoginPermission(BasePermission):
    def has_permission(self, request, view):
        if ("HTTP_ACCEPTANCES" in request.META) and (30 >= request.META.get("HTTP_ACCEPTANCES", str(None)).__len__() > 0):
            return True
        else:
            raise UserLoginPermissionError

    def has_object_permission(self, request, view, obj):
        return True
